#include "LayerCUnEvent.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "deprecated/CCDeprecated.h"
#include "ui/UIHelper.h"
#include "base/CCConsole.h"
#include "logic/CocosUtil.h"

USING_NS_CC;
using namespace ui;

#define Schedule_Key_BroadcastProperty1				"bcp_1"
#define Schedule_Key_BroadcastProperty2				"bcp_2"

#define Schedule_Delay_BroadcastProperty			0.2f


CLayerCUnEvent::CLayerCUnEvent(void) : m_Interval(0.00)
{
	m_PropertyParams.runNode = this;
	m_PropertyParams.startPos = cocos2d::Vec2(300, 600);
}


CLayerCUnEvent::~CLayerCUnEvent(void)
{
}

bool CLayerCUnEvent::init() {
	// 1. super init first
	if ( !CLayerBase::init() )
	{
		return false;
	}

	this->setContentSize(m_Director->getOpenGLView()->getFrameSize());
	this->setTouchEnabled(false);

	//m_Pack = CSLoader::createNode("Inventory.csb");

	//ui::Button* tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Pack, 93);
	//tLoginB->addTouchEventListener(CC_CALLBACK_2(CLayerCUnEvent::onOpenPack, this));

	//addChild(m_Pack);

	return true;
}

void CLayerCUnEvent::onEnter() {
	CLayerBase::onEnter();
	this->scheduleUpdate();
}

void CLayerCUnEvent::onExit() {
	CLayerBase::onExit();
	this->unscheduleUpdate();
}

void CLayerCUnEvent::update(float delta) {
	CLayerBase::update(delta);
	m_Interval += delta;
	if (m_Interval >= Schedule_Delay_BroadcastProperty) {
		m_Interval = 0.00;
		broadcastProperty(delta);
	}
}

void CLayerCUnEvent::BroadcastProperty(uint8_t count, PropertyInfo* prop) {
	for (uint8_t i = 0; i != count; ++i) {
		//if (prop->m_Type == )
		PropertyInfo tProp = *prop;
		m_NeedBroadcastProperty.push_back(tProp);
		++prop;
	}
	//CCLOG("~~~%d~~~~~", m_NeedBroadcastProperty.size());
	//m_Director->getScheduler()->performFunctionInCocosThread(CC_CALLBACK_0(CLayerCUnEvent::onBroadcastProperty, this));
}

/*
void CLayerCUnEvent::onOpenPack(cocos2d::Ref* pSender, cocos2d::ui::Widget::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::Widget::TouchEventType::ENDED:
		
		break;
	}
}*/

void CLayerCUnEvent::onBroadcastProperty() {
	std::string tStr1(Schedule_Key_BroadcastProperty1);
	std::string tStr2(Schedule_Key_BroadcastProperty2);
	if (!isScheduled(tStr1) && !isScheduled(tStr2))
		scheduleOnce(CC_CALLBACK_1(CLayerCUnEvent::onBroadcastOneProperty , this), Schedule_Delay_BroadcastProperty, tStr1);
}

void CLayerCUnEvent::onBroadcastOneProperty(float d) {
	broadcastProperty(d);
}

void CLayerCUnEvent::broadcastProperty(float d) {
	if (!m_NeedBroadcastProperty.empty()) {
		const PropertyInfo& tProperty = m_NeedBroadcastProperty.front();
		m_NeedBroadcastProperty.pop_front();
		m_PropertyParams.name = m_UiConfig->PropertyName[tProperty.m_Type];
		m_PropertyParams.flag = "+";
		m_PropertyParams.value = tProperty.m_Value;
		m_UiManager->RunPropertyBroadcast(m_PropertyParams, CCommonUiManager::NQIPropertyBroadcastReward);
	}
	//if (!m_NeedBroadcastProperty.empty()) {
	//	std::string tStr(Schedule_Key_BroadcastProperty2);
	//	scheduleOnce(CC_CALLBACK_1(CLayerCUnEvent::broadcastProperty , this), Schedule_Delay_BroadcastProperty, tStr);
	//}
}
